package com.angenela.lmz.controller;

import com.angenela.lmz.constant.Constant;
import com.angenela.lmz.dto.ObjectDTO;
import com.angenela.lmz.mapper.StarMapper;
import com.angenela.lmz.pojo.StarInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "收藏信息相关接口")
@RestController
@RequestMapping("/star")
public class StarController {
    @Autowired
    private StarMapper starMapper;
    @Autowired
    private ObjectMapper objectMapper;



    @ApiOperation("添加一个收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true),
            @ApiImplicitParam(name = "imageId", value = "图片id", required = true)
    })
    @PostMapping("/add")
    public String star(@RequestParam("userId") Integer userId, @RequestParam("imageId") Integer imageId) throws JsonProcessingException {
        ObjectDTO dto = new ObjectDTO();
        starMapper.starImage(userId, imageId);
        dto.setCode(Constant.SUCCESS_CODE);
        return objectMapper.writeValueAsString(dto);
    }



    @ApiOperation("取消一个收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true),
            @ApiImplicitParam(name = "imageId", value = "图片id", required = true)
    })
    @PostMapping("/delete")
    public String unStar(@RequestParam("userId") Integer userId, @RequestParam("imageId") Integer imageId) throws JsonProcessingException {
        ObjectDTO dto = new ObjectDTO();
        starMapper.unStarImage(userId, imageId);
        dto.setCode(Constant.SUCCESS_CODE);
        return objectMapper.writeValueAsString(dto);
    }



    @ApiOperation("根据用户id获取收藏信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true)
    })
    @GetMapping("/info/{userId}")
    public String unStar(@PathVariable("userId") Integer userId) throws JsonProcessingException {
        ObjectDTO dto = new ObjectDTO();
        List<StarInfo> starInfos = starMapper.getStarInfoByUserId(userId);
        dto.setData(starInfos);
        dto.setCode(Constant.SUCCESS_CODE);
        return objectMapper.writeValueAsString(dto);
    }
}
